package com.xej.seat.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xej.seat.mapper.CustomerMapper;
import com.xej.seat.model.SeatingType;
import com.xej.seat.service.CustomerService;
import com.xej.seat.vo.Customer;
import com.xej.seat.vo.Pager;
import com.xej.seat.vo.Seat;
@Service
public class CustomerServiceImpl implements CustomerService{
	protected static List<Customer> cusList = new ArrayList<Customer>();
	@Autowired
	private CustomerMapper customerMapper;
	
	@Override
	public Customer queryCustomerById(String cusId) {
		return this.customerMapper.queryCustomerById(cusId);
	}

	@Override
	public List<Customer> queryCustomerListByParams(Map<String, Object> params) {
		return this.customerMapper.queryCustomerListByParams(params);
	}

	 @Override
	    public List<Customer> loadCustomerList(Pager<Customer> pager, String cusName) {
        return customerMapper.loadCustomerList(pager, cusName);
	    }
	
	
	@Override
	public List<Customer> loadCustomer() {
//		if(cusList.size() == 0) {
//			this.setCusList();
//		}
		this.setCusList();
		return cusList;
	}
	
	protected void setCusList() {
		cusList = this.customerMapper.loadCustomer(null);
	}
	
	
	/**
     * 增加会员
     * @param Customer
    */
	@Override
	public int initCustomer(List<Customer> list) {
		return this.customerMapper.initCustomer(list);
	}
	
    @Override
    public void initPage(Pager pager) {
        int count = customerMapper.initPage(pager);
        pager.setTotalCount(count);
    }

	@Override
	public int updateCustomerManualById(String cusId) {
		return this.customerMapper.updateCustomerManualById(cusId);
	}

	@Override
	public int updateCustomerAuto() {
		return this.customerMapper.updateCustomerAuto();
	}
	
	public void setSeatFromCustomer(List<Seat> seats, List<Customer> customers) {
		if(seats != null && seats.size() > 0 && customers != null && customers.size() > 0) {
			for(int i=0, j=0; i<seats.size() && j<customers.size(); i++) {
				if(SeatingType.isManual(seats.get(i).getSeatingType())) {
					continue;
				}
				j = this.getCustomer(customers, j);
				Customer customer = customers.get(j);
				j ++;
				seats.get(i).setCusId(customer.getCusId());
				seats.get(i).setName(customer.getCusName());
				seats.get(i).setTitle(customer.getCusPosit());
				seats.get(i).setOrg(customer.getInstName());
				seats.get(i).setRow(seats.get(i).getSrow());
				seats.get(i).setSeatNo(seats.get(i).getSid());
				seats.get(i).setColumn(seats.get(i).getScol());
			}
		}
	}
	
	private int getCustomer(List<Customer> customers, int j) {
    	for (; j<customers.size(); j++) {
    		if(SeatingType.isManual(customers.get(j).getSeatingType())) {
    			continue;
    		} else {
    			break;
    		}
    	}
    	return j;
    }
	
}
